<template>
  <div>
    <el-dialog
      :title="isUpdate ? '编辑' : '新建'"
      v-model="props.visible"
      width="50%"
      @close="closeDialog()"
    >
      <el-form
        :model="form"
        label-position="right"
        label-width="100px"
        ref="formRef"
        @submit.prevent
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="模块:" prop="modelName">
              <el-select v-model="form.modelName">
                <el-option
                  v-for="item in modelList"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="taskId:" prop="taskId">
              <el-input
                placeholder="请输入taskId"
                v-model="form.taskId"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="taskName" prop="taskName">
              <el-input
                placeholder="请输入需求名称"
                v-model="form.taskName"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="分支:" prop="branch">
              <el-input
                placeholder="请输入分支"
                v-model="form.branch"
                :rows="6"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="开发人员：" prop="devCaseName">
              <el-select v-model="form.devCaseName">
                <el-option
                  v-for="item in developPersonnelList"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>

              <!-- <el-input
                placeholder="请输入人员"
                v-model="form.devCaseName"
              ></el-input> -->
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="开发时间：" prop="devUseTimeArr">
              <el-date-picker
                v-model="devUseTimeArr"
                type="daterange"
                range-separator="To"
                start-placeholder="Start date"
                end-placeholder="End date"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="测试人员:" prop="testCaseName">
              <el-select v-model="form.testCaseName">
                <el-option
                  v-for="item in testPersonnelList"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
              <!-- <el-input
                placeholder="请输入人员"
                v-model="form.testCaseName"
                :rows="6"
              ></el-input> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="测试时间:" prop="testUseTimeArr">
              <el-date-picker
                v-model="testUseTimeArr"
                type="daterange"
                range-separator="To"
                start-placeholder="Start date"
                end-placeholder="End date"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="上线时间:" prop="onlineTime">
              <el-date-picker
                v-model="form.onlineTime"
                type="date"
                placeholder="请选择上线日期"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="18" :offset="1">
            <el-form-item label="是否上线：" prop="isOnline">
              <el-switch
                v-model="form.isOnline"
                inline-prompt
                active-text="已上线"
                inactive-text="未上线"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="备注：" prop="remark">
              <el-input
                placeholder=""
                type="textarea"
                v-model="form.remark"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-row :gutter="20"
          ><el-col :span="17" :offset="1">
            <div
              class="dialog-footer"
              style="display: flex; justify-content: left; margin-bottom: 30px"
            >
              <el-button type="primary" @click="save()">确 定</el-button>
              <el-button @click="closeDialog()">取 消</el-button>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { tkTaskApi } from "@/api/kernel/tk-task.js";

const modelList = ref(["红线", "美盾", "网关", "客服"]);
const testPersonnelList = ref(["李文", "卢伟浩", "郭金淼"]);
const developPersonnelList = ref([
  "袁爱莉",
  "王付坤",
  "高国登",
  "王国伟",
  "易大为",
]);

const { proxy } = getCurrentInstance();

const formRef = ref(null);
const props = defineProps({
  visible: Boolean,
  data: Object,
});
const emits = defineEmits(["update:visible", "done"]);

const form = ref({
  isOnline: false,
});
const rules = ref({
  modelName: [{ required: true, message: "模块不能为空", trigger: "blur" }],
  taskId: [{ required: true, message: "taskId不能为空", trigger: "blur" }],
  taskName: [{ required: true, message: "taskName不能为空", trigger: "blur" }],
  dbUser: [
    { required: true, message: "数据库用户名不能为空", trigger: "blur" },
  ],
  dbPassword: [
    { required: true, message: "数据库密码不能为空", trigger: "blur" },
  ],
  dbPort: [{ required: true, message: "数据库端口不能为空", trigger: "blur" }],
});
onMounted(async () => {
  if (props.data) {
    getInfoById(props.data);
    isUpdate.value = true;
  } else {
    isUpdate.value = false;
  }
});
const getInfoById = (data) => {
  let { id } = data;
  tkTaskApi.getInfoById(id).then((res) => {
    if (res.data.devUseTimeArr != [] && res.data.devUseTimeArr != null) {
      devUseTimeArr.value = res.data.devUseTimeArr;
    }
    if (res.data.testUseTimeArr != [] && res.data.testUseTimeArr != null) {
      testUseTimeArr.value = res.data.testUseTimeArr;
    }
    form.value = Object.assign({}, res.data);
  });
};

// 是否是编辑状态
const isUpdate = ref(false);

const closeDialog = () => {
  formRef.value.resetFields();
  updateVisible(false);
  emits("done");
};
// 更改弹框状态
const updateVisible = (value) => {
  emits("update:visible", value);
};
let devUseTimeArr = ref([]);

let testUseTimeArr = ref([]);

//#- 保存
const loading = ref(false);

const save = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (devUseTimeArr.value != [] && devUseTimeArr.value != null) {
      let timeFormat = devUseTimeArr.value.join("/");
      form.value.devUseTime = timeFormat;
    } else {
      form.value.devUseTime = "";
    }
    if (testUseTimeArr.value != [] && testUseTimeArr.value != null) {
      let timeFormat = testUseTimeArr.value.join("/");
      form.value.testUseTime = timeFormat;
    } else {
      form.value.testUseTime = "";
    }

    if (valid) {
      loading.value = true;
      if (isUpdate.value) {
        tkTaskApi.edit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
        });
      } else {
        tkTaskApi.add(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
        });
      }

      loading.value = false;
      closeDialog();
    }
  });
};
</script>
